package dtl

import (
	"fmt"
	"io"

	"github.com/jacobsa/go-serial/serial"
)

type Dtl645 interface {
	GetMeterComandResult(meterid string, comand string, rw io.ReadWriteCloser, lenght int, xs float64) (err error, values []float64)
	ReadMeterId(Meter *Meter, s io.ReadWriteCloser) (err error)
	GetKvi(Meter *Meter, s io.ReadWriteCloser) (err error)
}

func OpenPort(portname string) (s io.ReadWriteCloser, err error) {
	// 打开端口
	c := serial.OpenOptions{
		PortName:              portname,
		BaudRate:              2400,
		DataBits:              8,
		StopBits:              1,
		ParityMode:            serial.PARITY_EVEN,
		InterCharacterTimeout: 2000,
		MinimumReadSize:       128,
	}
	fmt.Println("打开端口:", c)
	s, err = serial.Open(c)
	return s, err
	// 获取总功率
}

type Meter struct {
	MeterAddr string
	MeterType string
	UV        float64
	VV        float64
	WV        float64
	UI        float64
	VI        float64
	WI        float64
	// 功率因素
	PF float64
	// 总功率
	PT float64
	// 倍率
	BM     float64
	Uptime string
}
